#include "stdafx.h"

#include "TextureResourceLoader.h"

#include "..\ExtraData\D3D11TextureExtraResourceData.h"

unsigned int TextureResourceLoader::GetLoadedResourceSize(
	char* pRawBuffer, 
	unsigned int rawSize)
{
	// This will keep the resource cache from allocating memory for the texture, 
	// so DirectX can manage it on it's own.
	return 0;
}

bool TextureResourceLoader::LoadResource(
	char* pRawBuffer, 
	unsigned int rawSize, 
	shared_ptr<ResourceHandle> handle)
{
	shared_ptr<D3D11TextureExtraResourceData> extra = 
		shared_ptr<D3D11TextureExtraResourceData>(
			TWF_NEW D3D11TextureExtraResourceData());

	// Load the Texture
	if ( FAILED ( D3DX11CreateShaderResourceViewFromMemory(
		DXUTGetD3D11Device(), 
		pRawBuffer, 
		rawSize, 
		NULL, 
		NULL, 
		&extra->_pTexture,
		NULL)))
	{
		return false;
	}

	// Create the sample state
	D3D11_SAMPLER_DESC sampDesc;
	ZeroMemory( &sampDesc, sizeof(sampDesc) );
	sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
	sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
	sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
	sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
	sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
	sampDesc.MinLOD = 0;
	sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
	if( FAILED(
		DXUTGetD3D11Device()->CreateSamplerState( 
			&sampDesc, 
			&extra->_pSamplerLinear)))
	{
		return false;
	}
	
	handle->SetExtra(shared_ptr<D3D11TextureExtraResourceData>(extra));
	
	return true;
}